Skip to content

fix(perps): use live price for reverse position orders#28897

Merged
aganglada merged 2 commits into
mainfrom
perps/fix-reverse-order-price
Apr 16, 2026
Merged

fix(perps): use live price for reverse position orders#28897
aganglada merged 2 commits into
mainfrom
perps/fix-reverse-order-price

Conversation

@geositta
Copy link
Copy Markdown
Contributor

@geositta geositta commented Apr 16, 2026

Description

Fixes reverse-position failures in perps by stopping flipPosition() from sending the position entry price as the order currentPrice.

The reverse-position flow was using stale entry pricing during order placement, which could cause the provider to validate and submit the flip order against the wrong price. This change makes the flip flow rely on live provider pricing instead, and updates the related regression coverage and analytics expectation.

Changelog

CHANGELOG entry: Fixed a bug that was causing reverse position orders in Perps to fail when stale entry pricing was used during order placement

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2946

Manual testing steps

  Feature: Reverse perps position

    Scenario: user reverses an open long position
      Given the user has an open long perps position
      And the user opens the reverse position flow
      When the user confirms the reverse position order
      Then the order is submitted successfully
      And the reverse order does not fail because of stale entry pricing

    Scenario: regression coverage for flipPosition
      Given the perps controller test suite is available
      When `yarn jest app/controllers/perps/services/TradingService.test.ts -t flipPosition` is run
      Then the tests pass

Screenshots/Recordings

Before

After

Pre-merge author checklist

Performance checks (if applicable)

  • I've tested on Android
    • Ideally on a mid-range device; emulator is acceptable
  • I've tested with a power user scenario
    • Use these power-user SRPs to import wallets with many accounts and tokens
  • I've instrumented key operations with Sentry traces for production performance metrics

For performance guidelines and tooling, see the Performance Guide.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Medium Risk
Touches order submission behavior for position flips, which can impact execution/validation if any provider implicitly relied on currentPrice; otherwise the change is small and covered by updated unit tests.

Overview
Fixes Perps reverse/flip orders by removing the currentPrice (position entryPrice) field from TradingService.flipPosition() order submissions so the provider validates/prices using live market data.

Updates flip analytics to compute order_value from the executed average price (falling back to entry price if missing), and adjusts TradingService.test.ts by deleting the old balance/fee-guard tests and asserting currentPrice is never passed.

Reviewed by Cursor Bugbot for commit b78150d. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions
Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot metamaskbot added the team-perps Perps team label Apr 16, 2026
@geositta geositta marked this pull request as ready for review April 16, 2026 06:39
@geositta geositta requested a review from a team as a code owner April 16, 2026 06:39
@github-actions github-actions Bot added the risk-medium Moderate testing recommended · Possible bug introduction risk label Apr 16, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokePerps, SmokeWalletPlatform, SmokeConfirmations
  • Selected Performance tags: None (no tests recommended)
  • Risk Level: medium
  • AI Confidence: 92%
click to see 🤖 AI reasoning details

E2E Test Selection:
The changes are isolated to TradingService.ts in the perps controller, specifically the flipPosition method. Two key behavioral changes were made: (1) removed pre-flight balance validation that checked account state before placing a flip order, and (2) removed passing the position's entry price as currentPrice to the order params, instead letting the provider fetch live market data. Analytics tracking was also updated to use the actual executed price (averagePrice) rather than the stale entry price. The only importer of TradingService is PerpsController.ts, so the blast radius is limited to perps functionality. SmokePerps is the primary tag for perpetuals trading. Per the tag descriptions, SmokePerps requires SmokeWalletPlatform (Perps is embedded in Trending tab) and SmokeConfirmations (Add Funds deposits are on-chain transactions).

Performance Test Selection:
The changes are purely logic-level modifications to the flipPosition method in TradingService — removing a balance validation check and changing how the order price is determined. There are no UI rendering changes, no list/component changes, no data loading pipeline changes, and no app startup/initialization changes. Performance tests are not warranted.

View GitHub Actions results

Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b78150d. Configure here.

PERPS_EVENT_PROPERTY,
PERPS_EVENT_VALUE,
} from '../constants/eventNames';
import { ESTIMATED_FEE_RATE } from '../constants/hyperLiquidConfig';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused export ESTIMATED_FEE_RATE after removing its only consumer

Low Severity

The ESTIMATED_FEE_RATE import was removed from TradingService.ts, but the constant is still exported from hyperLiquidConfig.ts. A codebase-wide grep confirms zero remaining consumers — it is now dead code. Its comment explicitly ties it to "flip operations," which no longer use it.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b78150d. Configure here.

@github-actions
Copy link
Copy Markdown
Contributor

E2E Fixture Validation — Schema is up to date
11 value mismatches detected (expected — fixture represents an existing user).
View details

@sonarqubecloud
Copy link
Copy Markdown

@abretonc7s abretonc7s enabled auto-merge April 16, 2026 12:06
@abretonc7s abretonc7s disabled auto-merge April 16, 2026 12:06
@abretonc7s abretonc7s added this pull request to the merge queue Apr 16, 2026
@abretonc7s abretonc7s removed this pull request from the merge queue due to a manual request Apr 16, 2026
@aganglada aganglada added this pull request to the merge queue Apr 16, 2026
Merged via the queue into main with commit 17417a6 Apr 16, 2026
103 checks passed
@aganglada aganglada deleted the perps/fix-reverse-order-price branch April 16, 2026 16:56
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 16, 2026
@metamaskbotv2 metamaskbotv2 Bot added the release-7.74.0 Issue or pull request that will be included in release 7.74.0 label Apr 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.74.0 Issue or pull request that will be included in release 7.74.0 risk-medium Moderate testing recommended · Possible bug introduction risk size-M team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants